package com.molichuxing.gateway.api.services.impl;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.result.ResponseResult;
import com.molichuxing.framework.utils.DateUtil;
import com.molichuxing.framework.utils.DownloadFileUtil;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.gateway.api.config.OptionalLogConfig;
import com.molichuxing.gateway.api.services.StockApiService;
import com.molichuxing.gateway.bff.dealer.entity.request.create.FenceCreateVo;
import com.molichuxing.gateway.bff.dealer.entity.request.modify.FenceModifyVo;
import com.molichuxing.gateway.bff.dealer.entity.response.FenceVo;
import com.molichuxing.gateway.bff.dealer.services.FenceBffService;
import com.molichuxing.gateway.bff.stock.entity.request.create.EntrepotStorageCreateVo;
import com.molichuxing.gateway.bff.stock.entity.request.modify.EntrepotStorageModifyVo;
import com.molichuxing.gateway.bff.stock.entity.request.modify.StockCarModifyVo;
import com.molichuxing.gateway.bff.stock.entity.response.EntrepotStorageBaseVo;
import com.molichuxing.gateway.bff.stock.entity.response.EntrepotStorageVo;
import com.molichuxing.gateway.bff.stock.entity.response.FenceBaseVo;
import com.molichuxing.gateway.bff.stock.entity.response.StockCarDetailVo;
import com.molichuxing.gateway.bff.stock.entity.response.StockCarStockVo;
import com.molichuxing.gateway.bff.stock.entity.response.StockCarVo;
import com.molichuxing.gateway.bff.stock.entity.response.StockRecordBaseVo;
import com.molichuxing.gateway.bff.stock.entity.response.StockRecordVo;
import com.molichuxing.gateway.bff.stock.services.EntrepotStorageBffService;
import com.molichuxing.gateway.bff.stock.services.StockCarBffService;
import com.molichuxing.gateway.bff.stock.services.StockRecordBffService;
import com.molichuxing.gateway.utils.PoiExcelUtil;
import com.molichuxing.gateway.utils.TokenUtil;
import com.molichuxing.services.property.FenceStoreStockTypeEnum;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author liyg
 * 库存车辆web接口实现类
 * 2019-08-26
 *
 */
@Api(tags = "库存相关接口")
@RestController
@RequestMapping(value = "/stock")
public class StockApiServiceImpl extends RootApiService implements StockApiService{
	
	private static final Logger logger = LoggerFactory.getLogger(StockApiServiceImpl.class);
	
	private static final Integer DOWNLOAD_MAX_COUNT = 10000;
	
	@Resource
	private StockCarBffService stockCarBffService;
	
	@Resource
	private StockRecordBffService stockRecordBffService;
	
	@Resource
	private EntrepotStorageBffService entrepotStorageBffService;
	
	@Resource
	private TokenUtil tokenUtil;

	@Resource
	FenceBffService fenceBffService;


	/**
	 * 获取库存列表
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取库存列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "tabType", value = "tab类型", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "vin", value = "车架号", required = false, dataType = "String"),
			@ApiImplicitParam(name = "licencePlate", value = "车牌号", required = false, dataType = "String"),
			@ApiImplicitParam(name = "operationState", value = "运营状态", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "type", value = "出入库类型", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "vehicleId", value = "车型id", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "stockType", value = "库类型（1、中转库 2、门店）", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "stockId", value = "中转库id", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String"),
			@ApiImplicitParam(name = "sortName", value = "排序字段", required = false, dataType = "String"),
			@ApiImplicitParam(name = "sortWay", value = "排序方式 ", required = false, dataType = "String"),
			@ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")
	})
	@GetMapping(value = "/in/list")
	public ResponseResult<Paged<StockCarVo>> getStockCarList(@RequestParam Map<String, Object> map) {
		
		ResponseResult<Paged<StockCarVo>> result = new ResponseResult<>();
		try {
			Integer pageNum = ObjectCastUtil.castInteger(map.get("pageNum"));
			if(null==pageNum || pageNum < 1 ) {
				map.put("pageNum",1);
			}
			Integer pageSize = ObjectCastUtil.castInteger(map.get("pageSize"));
			if(null==pageSize || pageSize <= 0) {
				map.put("pageSize",10);
			}
			String startTime = ObjectCastUtil.castString(map.get("startTime"));
			if(StringUtils.isNoneBlank(startTime)) {
				map.put("startTime",DateUtil.parseDate(startTime).atStartOfDay());
			}
			String endTime = ObjectCastUtil.castString(map.get("endTime"));
			if(StringUtils.isNoneBlank(endTime)) {
				map.put("endTime", DateUtil.getDayEnd(DateUtil.parseDate(endTime).atStartOfDay()));
			}
			
			return result.success(stockCarBffService.getStockCarList(map));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getStockCarList]获取库存列表信息异常", e);
			result.error("获取库存列表信息异常");
		}
		return result;
	}
	
	/**
	 * 获取车辆库存列表（new）
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取库存列表（new）")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "vin", value = "车架号", required = false, dataType = "String"),
			@ApiImplicitParam(name = "licencePlate", value = "车牌号", required = false, dataType = "String"),
			@ApiImplicitParam(name = "status", value = "运营状态", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "vehicleId", value = "车型id", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "vehicleType", value = "车辆类型", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")
	})
	@GetMapping(value = "/stock/list")
	public ResponseResult<Paged<StockCarStockVo>> getStockCarPage(@RequestParam Map<String, Object> map) {
		
		ResponseResult<Paged<StockCarStockVo>> result = new ResponseResult<>();
		try {
			Integer pageNum = ObjectCastUtil.castInteger(map.get("pageNum"));
			if(null==pageNum || pageNum < 1 ) {
				map.put("pageNum",1);
			}
			Integer pageSize = ObjectCastUtil.castInteger(map.get("pageSize"));
			if(null==pageSize || pageSize <= 0) {
				map.put("pageSize",10);
			}
			
			return result.success(stockCarBffService.getStockCarPage(map));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getStockCarList]获取库存列表信息异常", e);
			result.error("获取库存列表信息异常");
		}
		return result;
	}
	
	/**
	 *获取库存车辆出库详情
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取库存车辆出库详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "carId", value = "车辆id", required = true, dataType = "Integer")

	})
	@GetMapping(value = "/out/detail")
	public ResponseResult<StockCarVo> getStockCar(@RequestParam Integer carId) {
		
		ResponseResult<StockCarVo> result = new ResponseResult<>();
		try {
			return result.success(stockCarBffService.getStockCar(carId));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getStockCar]获取库存车辆出库详情异常", e);
			result.error("获取库存车辆出库详情异常");
		}
		return result;
	}
	
	/**
	 *获取库存车辆出库详情
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取库存车辆入库详情")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "carId", value = "车辆id", required = true, dataType = "Integer")
		
	})
	@GetMapping(value = "/in/detail")
	public ResponseResult<StockCarDetailVo> getStockCarDetail(@RequestParam Integer carId) {
		
		ResponseResult<StockCarDetailVo> result = new ResponseResult<>();
		try {
			return result.success(stockCarBffService.getStockCarDetail(carId));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getStockCarDetail]获取库存车辆出库详情异常", e);
			result.error("获取库存车辆出库详情异常");
		}
		return result;
	}
	
	/**
	 *获取库存记录列表
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取库存记录列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "vin", value = "车架号", required = false, dataType = "String"),
			@ApiImplicitParam(name = "licencePlate", value = "车牌号", required = false, dataType = "String"),
			@ApiImplicitParam(name = "type", value = "出入库类型", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "vehicleId", value = "车型id", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")

	})
	@GetMapping(value = "/in/out/list")
	public ResponseResult<Paged<StockRecordVo>> getStockRecordList(@RequestParam Map<String, Object> map) {
		
		ResponseResult<Paged<StockRecordVo>> result = new ResponseResult<>();
		try {
			Integer pageNum = ObjectCastUtil.castInteger(map.get("pageNum"));
			if(null==pageNum || pageNum < 1 ) {
				map.put("pageNum",1);
			}
			Integer pageSize = ObjectCastUtil.castInteger(map.get("pageSize"));
			if(null==pageSize || pageSize <= 0) {
				map.put("pageSize",10);
			}
			return result.success(stockRecordBffService.getStockRecordList(map));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getStockRecordList]获取库存记录列表信息异常", e);
			result.error("获取库存记录列表信息异常");
		}
		return result;
	}
	
	/**
	 *根据车辆id获取库存历史流转记录
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "根据车辆id获取库存（车辆）历史流转记录")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "carId", value = "车辆id", required = true, dataType = "Integer")
	})
	@GetMapping(value = "/moving/list")
	public ResponseResult<List<StockRecordBaseVo>> getListByCarId(@RequestParam Integer carId) {
		
		ResponseResult<List<StockRecordBaseVo>> result = new ResponseResult<>();
		try {
			return result.success(stockRecordBffService.getListByCarId(carId));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getListByCarId]根据车辆id获取库存（车辆）历史流转记录异常", e);
			result.error("根据车辆id获取库存（车辆）历史流转记录异常");
		}
		return result;
	}
	
	/**
	 * 车辆入库信息
	 * @param modifyInStock
	 * @return
	 * 
	 */
	@Override
	@ApiOperation(value = "车辆入库信息")
	@OptionalLogConfig(methods="入库")
	@PostMapping(value = "/in/create")
	public ResponseResult<Boolean> modifyInStock(@RequestBody StockCarModifyVo modifyVo){
		ResponseResult<Boolean>  result = new ResponseResult<>();
		try {
			return result.success(stockCarBffService.modifyInStock(modifyVo));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[modifyInStock]车辆入库信息异常", e);
			result.error("车辆入库信息异常");
		}
	
		return result;
	}
	
	/**
	 *车辆出库信息
	 * @param modifyInStock
	 * @return
	 * 
	 */
	@Override
	@ApiOperation(value = "车辆出库信息")
	@OptionalLogConfig(methods="出库")
	@PostMapping(value = "/out/create")
	public ResponseResult<Boolean> modifyOutStock(@RequestBody StockCarModifyVo modifyVo){
		ResponseResult<Boolean>  result = new ResponseResult<>();
		try {
			return result.success(stockCarBffService.modifyOutStock(modifyVo));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[modifyOutStock]车辆出库信息异常", e);
			result.error("车辆出库信息异常");
		}
		return result;
	}
	
	/**
	 *确认回库信息
	 * @param modifyInStock
	 * @return
	 * 
	 */
	@Override
	@ApiOperation(value = "确认回库信息")
	@OptionalLogConfig(methods="确认回库")
	@PostMapping(value = "/return/confirm/create")
	public ResponseResult<Boolean> modifyCollectStoreStock(@RequestBody StockCarModifyVo modifyVo){
		ResponseResult<Boolean>  result = new ResponseResult<>();
		try {
			return result.success(stockCarBffService.modifyCollectStoreStock(modifyVo));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[modifyCollectStoreStock]确认回库信息异常", e);
			result.error("确认回库信息异常");
		}
		return result;
	}
	
	/**
	 *收车 车到中转库
	 * @param modifyInStock
	 * @return
	 * 
	 */
	@Override
	@ApiOperation(value = "收车 车到中转库信息")
	@OptionalLogConfig(methods="收车")
	@PostMapping(value = "/return/create")
	public ResponseResult<Boolean> modifyCollectStock(@RequestBody StockCarModifyVo modifyVo){
		ResponseResult<Boolean>  result = new ResponseResult<>();
		try {
			return result.success(stockCarBffService.modifyCollectStock(modifyVo));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[modifyCollectStock]收车 车到中转库信息异常", e);
			result.error("收车 车到中转库信息异常");
		}
		return result;
	}
	
	/**
	 *获取中转库列表
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取中转库列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "shortName", value = "中转库简称", required = false, dataType = "String"),
			@ApiImplicitParam(name = "name", value = "负责人姓名", required = false, dataType = "String"),
			@ApiImplicitParam(name = "regionId", value = "城市id", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "sortName", value = "排序字段", required = false, dataType = "String"),
			@ApiImplicitParam(name = "sortWay", value = "排序方式", required = false, dataType = "String"),
			@ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")

	})
	@GetMapping(value = "/entrepot/list")
	public ResponseResult<Paged<EntrepotStorageVo>> getEntrepotStorageList(@RequestParam Map<String, Object> map) {
		
		ResponseResult<Paged<EntrepotStorageVo>> result = new ResponseResult<>();
		try {
			Integer pageNum = ObjectCastUtil.castInteger(map.get("pageNum"));
			if(null==pageNum || pageNum < 1 ) {
				map.put("pageNum",1);
			}
			Integer pageSize = ObjectCastUtil.castInteger(map.get("pageSize"));
			if(null==pageSize || pageSize <= 0) {
				map.put("pageSize",10);
			}
			return result.success(entrepotStorageBffService.getEntrepotStorageList(map));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getEntrepotStorageList]获取中转库列表信息异常", e);
			result.error("获取中转库列表信息异常");
		}
		return result;
	}
	
	/**
	 *模糊查询中转库或门店信息
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "模糊查询中转库或门店信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "shortName", value = "名称简称", required = false, dataType = "String"),
		@ApiImplicitParam(name = "stockType", value = "类型（1：中转库，2：门店）", required = false, dataType = "Integer"),
		@ApiImplicitParam(name = "storeStatus", value = "门店营业状态（null：所有门店，1：门店营业中）", required = false, dataType = "Integer"),
		@ApiImplicitParam(name = "dealerId", value = "经销商id", required = false, dataType = "Integer"),
		@ApiImplicitParam(name = "dealerStatus", value = "经销商营业状态（null：所有门店，1：经销商营业中）", required = false, dataType = "Integer"),
		@ApiImplicitParam(name = "isShow", value = " 删除是否显示(null:所有，0:否，1：是)", required = false, dataType = "Integer"),
		@ApiImplicitParam(name = "dealerType", value = "经销商类型(null:所有，1:直销，2：分销)", required = true, dataType = "Integer")
		
	})
	@GetMapping(value = "/entrepot/store/list")
	public ResponseResult<List<EntrepotStorageBaseVo>> getEntrepotOrStore(@RequestParam Map<String, Object> map) {
		
		ResponseResult<List<EntrepotStorageBaseVo>> result = new ResponseResult<>();
		try {
			return result.success(entrepotStorageBffService.getEntrepotOrStore(map));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getEntrepotOrStore]模糊查询中转库或门店信息异常", e);
			result.error("模糊查询中转库或门店信息异常");
		}
		return result;
	}
	
	
	/**
	 * 根据门店或者中转库  id 查询 围栏数据
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "根据门店或者中转库查 围栏数据信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "门店id或中转库id", required = false, dataType = "Integer"),
		@ApiImplicitParam(name = "stockType", value = "类型（1：中转库，2：门店）", required = false, dataType = "Integer")
	})
	@GetMapping(value = "/entrepot/store/fence")
	public ResponseResult<List<FenceBaseVo>> getEntrepotOrStoreFence(@RequestParam Map<String, Object> map) {
		
		ResponseResult<List<FenceBaseVo>> result = new ResponseResult<>();
		try {
			return result.success(entrepotStorageBffService.getEntrepotOrStoreFence(map));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getEntrepotOrStoreFence]根据门店或者中转库查 围栏数据信息异常", e);
			result.error("根据门店或者中转库查 围栏数据信息异常");
		}
		return result;
	}
	
	/**
	 *获取中转库下拉列表
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取中转库下拉列表")
	@GetMapping(value = "/entrepot/base/list")
	public ResponseResult<List<EntrepotStorageVo>> getList(){
		
		ResponseResult<List<EntrepotStorageVo>> result = new ResponseResult<>();
		try {
			return result.success(entrepotStorageBffService.getEntrepotStorageList());
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getEntrepotStorageList]获取中转库下拉列表信息异常", e);
			result.error("获取中转库下拉列表信息异常");
		}
		return result;
	}
	
	/**
	 *获取中转库下拉列表
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取中转库下拉列表(初始化围栏id)")
	@GetMapping(value = "/entrepot/base/lists")
	public ResponseResult<List<EntrepotStorageVo>> getLists(){
		
		ResponseResult<List<EntrepotStorageVo>> result = new ResponseResult<>();
		try {
			return result.success(entrepotStorageBffService.getEntrepotStorageLists());
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getEntrepotStorageList]获取中转库下拉列表信息异常", e);
			result.error("获取中转库下拉列表信息异常");
		}
		return result;
	}
	
	/**
	 *获取中转库详情
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取中转库详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "中转库id", required = true, dataType = "Integer")

	})
	@GetMapping(value = "/entrepot/detail")
	public ResponseResult<EntrepotStorageVo> getEntrepotStorage(@RequestParam Integer  id) {
		
		ResponseResult<EntrepotStorageVo> result = new ResponseResult<>();
		try {
			
			return result.success(entrepotStorageBffService.getEntrepotStorage(id));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getEntrepotStorage]获取中转库详情信息异常", e);
			result.error("获取中转库详情信息异常");
		}
		return result;
	}
	
	
	/**
	 * 保存中转库信息
	 * @param createDto
	 * @return
	 * 
	 */
	@Override
	@ApiOperation(value = "保存中转库信息")
	@OptionalLogConfig(methods="添加中转库信息")
	@PostMapping(value = "/entrepot/create")
	public ResponseResult<Boolean> create(@RequestBody EntrepotStorageCreateVo createVo){
		ResponseResult<Boolean>  result = new ResponseResult<>();
		try {
			return result.success(entrepotStorageBffService.create(createVo));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[create]保存中转库信息异常", e);
			result.error("保存中转库信息异常");
		}
		return result;
	}
	
	/**
	 * 修改中转库信息
	 * @param createDto
	 * @return
	 * 
	 */
	@Override
	@ApiOperation(value = "修改中转库信息")
	@OptionalLogConfig(methods="编辑中转库信息")
	@PostMapping(value = "/entrepot/modify")
	public ResponseResult<Boolean> modify(@RequestBody EntrepotStorageModifyVo modifyVo){
		ResponseResult<Boolean>  result = new ResponseResult<>();
		try {
			return result.success(entrepotStorageBffService.modify(modifyVo));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[modify]修改中转库信息异常", e);
			result.error("修改中转库信息异常");
		}
		return result;
	}
	
	@Override
	@ApiOperation(value = "获取库存列表导出")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "tabType", value = "tab类型", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "ids", value = "库存ids", required = false, dataType = "String"),
			@ApiImplicitParam(name = "vin", value = "车架号", required = false, dataType = "String"),
			@ApiImplicitParam(name = "licencePlate", value = "车牌号", required = false, dataType = "String"),
			@ApiImplicitParam(name = "operationState", value = "运营状态", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "type", value = "出入库类型", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "vehicleId", value = "车型id", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "stockType", value = "库类型（1、中转库 2、门店）", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "stockId", value = "中转库id", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String")
	})
	@GetMapping(value = "/download/in/list")
	public void getStockCarDownloadList(HttpServletRequest request, HttpServletResponse response,
			@RequestParam Map<String, Object> map) {
		try {
			String fileName = null;
			SXSSFWorkbook sxssBook = null;
			List<StockCarVo> list = stockCarBffService.getStockCarDownloadList(map);
			fileName = "仓储作业列表(" + DateUtil.formatDate(LocalDate.now()) + ").xlsx";
			// 生成excel
			sxssBook = PoiExcelUtil.getStockCarDownloadList(list);	
		
			// 下载
			DownloadFileUtil.downloadExcel(request, response, fileName, sxssBook);
		} catch (BizException e) {
			logger.error("[getStockCarDownloadList]库存列表导出异常:", e);
		} catch (Exception e) {
			logger.error("[getStockCarDownloadList]库存列表导出导出异常:", e);
		}
	}
	
	@Override
	@ApiOperation(value = "获取库存列表导出查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "tabType", value = "tab类型", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "ids", value = "库存ids", required = false, dataType = "String"),
			@ApiImplicitParam(name = "vin", value = "车架号", required = false, dataType = "String"),
			@ApiImplicitParam(name = "licencePlate", value = "车牌号", required = false, dataType = "String"),
			@ApiImplicitParam(name = "operationState", value = "运营状态", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "type", value = "出入库类型", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "vehicleId", value = "车型id", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "stockType", value = "库类型（1、中转库 2、门店）", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "stockId", value = "中转库id", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String")
	})
	@GetMapping(value = "/download/in/list/query")
	public ResponseResult<Boolean> getStockCarDownloadListQuery(HttpServletRequest request, HttpServletResponse response,
			@RequestParam Map<String, Object> map) {
		ResponseResult<Boolean> result = new ResponseResult<>();
		try {
			Long count = stockCarBffService.getStockCarDownloadListCount(map);
			if (count != null && count > DOWNLOAD_MAX_COUNT) {
				// 发送邮件-异步请求
				stockCarBffService.getStockCarDownloadListEmail(map,tokenUtil.getUser());
				// 返回提示
				result.success("已将导出数据发送至您的邮箱", true);
			} else {
				// 小于最大导出条数，提示前端调用直接导出接口
				result.success(false);
			}
		} catch (BizException e) {
			logger.error("[getStockCarDownloadList]库存列表导出异常:", e);
			result.error("仓储作业信息导出异常");
		} catch (Exception e) {
			logger.error("[getStockCarDownloadList]库存列表导出导出异常:", e);
			result.error("仓储作业信息导出异常");
		}
		return result;
	}

	/**
	 * 获取已有围栏列表
	 *
	 * @param storeId
	 * @return
	 */
	@Override
	@ApiOperation(value = "查询已有围栏列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "storeId", value = "门店或中转库id", required = true, dataType = "Integer"),
	})
	@GetMapping(value = "/entrepot/fence/binding/list")
	public ResponseResult<List<FenceVo>> getBindingFenceList(@RequestParam Integer storeId) {
		ResponseResult<List<FenceVo>> result = new ResponseResult<>();
		try {
			return result.success(fenceBffService.getBindingFenceList(storeId, FenceStoreStockTypeEnum.ENTREPOT_STORAGE.getValue()));
		} catch (IllegalArgumentException e) {
			logger.error("[getBindingFenceList]获取已有围栏异常", e);
			result.error(e.getMessage());
		} catch (BizException e) {
			logger.error("[getBindingFenceList]获取已有围栏异常", e);
			result.error(e.getMessage());
		} catch (Exception e) {
			logger.error("[getBindingFenceList]获取已有围栏异常 ", e);
			result.error("获取已有围栏异常");
		}
		return result;
	}

	/**
	 * 获取可添加围栏列表
	 *
	 * @param storeId
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取可添加围栏列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "storeId", value = "门店或中转库id", required = true, dataType = "Integer"),
	})
	@GetMapping(value = "/entrepot/fence/unbinding/list")
	public ResponseResult<List<FenceVo>> getUnBindingFenceList(@RequestParam Integer storeId) {
		ResponseResult<List<FenceVo>> result = new ResponseResult<>();
		try {
			return result.success(fenceBffService.getUnBindingFenceList(storeId, FenceStoreStockTypeEnum.ENTREPOT_STORAGE.getValue()));
		} catch (IllegalArgumentException e) {
			logger.error("[getUnBindingFenceList]获取可添加围栏异常", e);
			result.error(e.getMessage());
		} catch (BizException e) {
			logger.error("[getUnBindingFenceList]获取可添加围栏异常", e);
			result.error(e.getMessage());
		} catch (Exception e) {
			logger.error("[getUnBindingFenceList]获取可添加围栏异常 ", e);
			result.error("获取可添加围栏异常");
		}
		return result;
	}

	/**
	 * 解除围栏
	 *
	 * @param fenceModifyVo
	 * @return
	 */
	@Override
	@ApiOperation(value = "解除围栏")
	@OptionalLogConfig(methods="中转库解除围栏")
	@PostMapping(value = "/entrepot/fence/delete")
	public ResponseResult<Boolean> deleteFence(@RequestBody FenceModifyVo fenceModifyVo) {
		ResponseResult<Boolean> result = new ResponseResult<>();
		try {
			int type = FenceStoreStockTypeEnum.ENTREPOT_STORAGE.getValue();
			fenceModifyVo.setType(type);
			return result.success(fenceBffService.deleteFence(fenceModifyVo));
		} catch (IllegalArgumentException e) {
			logger.error("[deleteFence]解除围栏异常", e);
			result.error(e.getMessage());
		} catch (BizException e) {
			logger.error("[deleteFence]解除围栏异常", e);
			result.error(e.getMessage());
		} catch (Exception e) {
			logger.error("[deleteFence]解除围栏异常 ", e);
			result.error("解除围栏异常");
		}
		return result;
	}

	/**
	 * 添加围栏
	 *
	 * @param fenceCreateVo
	 * @return
	 */
	@Override
	@ApiOperation(value = "添加围栏")
	@OptionalLogConfig(methods="中转库添加围栏")
	@PostMapping(value = "/entrepot/fence/create")
	public ResponseResult<Boolean> createFence(@RequestBody FenceCreateVo fenceCreateVo) {
		ResponseResult<Boolean> result = new ResponseResult<>();
		try {
			int type = FenceStoreStockTypeEnum.ENTREPOT_STORAGE.getValue();
			fenceCreateVo.setType(type);
			return result.success(fenceBffService.createFence(fenceCreateVo));
		} catch (IllegalArgumentException e) {
			logger.error("[createFence]添加围栏异常", e);
			result.error(e.getMessage());
		} catch (BizException e) {
			logger.error("[createFence]添加围栏异常", e);
			result.error(e.getMessage());
		} catch (Exception e) {
			logger.error("[createFence]添加围栏异常 ", e);
			result.error("添加围栏异常");
		}
		return result;
	}

}
